<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        管理
        <small>列表</small>
    </h1>
</section>

<!-- Main content -->
<section class="content" id="app-version-app">

    <div class="row">
        <div class="col-md-6">
            <a class="btn btn-default" @click="getList">刷新</a>
            <a v-show="!show.edit" class="btn btn-info" @click="add(1)">添加Android版本</a>
            <a v-show="!show.edit" class="btn btn-info" @click="add(2)">添加IOS版本</a>
            <a v-show="show.edit" class="btn btn-danger" @click="show.edit = false">关闭</a>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label>选择平台</label>
                <select v-model="page.platform">
                    <option value="0">全部平台</option>
                    <option value="1">Android</option>
                    <option value="2">IOS</option>
                </select>
            </div>
        </div>
    </div>

    <div class="row" v-show="!show.edit">
        <div class="col-md-12">
            <table class="table table-responsive table-striped table-bordered">
                <thead>
                <tr>
                    <th>平台</th>
                    <th>版本号</th>
                    <th>版本名</th>
                    <th>级别</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="item in list">
                    <td>{{edit_item.platform==1 ? 'Android' : 'IOS'}}</td>
                    <td>{{item.code}}</td>
                    <td>{{item.version}}</td>
                    <td>{{item.important | Important}}</td>
                    <td>{{item.created}}</td>
                    <td>
                        <a class="btn btn-primary btn-xs" @click="edit(item)">编辑</a>
                        <a class="btn btn-danger btn-xs" @click="del(item)">删除</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="100">
                        <page :rows="page.rows" :last="page.last" v-model="page.page"></page>
                    </td>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row" v-show="show.edit">
        <div class="col-md-4 col-md-offset-2">
            <h3>编辑{{edit_item.platform==1 ? 'Android' : 'IOS'}}</h3>
            <div class="form" role="form">

                <div class="form-group">
                    <label>版本号(用于判断是否更新，不能重复)：</label>
                    <input class="form-control" v-model="edit_item.code" type="number" min="1" step="1">
                </div>

                <div class="form-group">
                    <label>版本名称(用于展示，不能重复)：</label>
                    <input class="form-control" v-model="edit_item.version">
                </div>

                <div class="form-group" v-if="1 == edit_item.platform">
                    <label>上传apk包：</label>
                    <file-uploader v-model="edit_item.apk_url" filetype="apk" oss="true" filesize="100M" policyUrl="/alioss/get_policy"></file-uploader>
                </div>

                <div class="form-group ">
                    <label>更新强度级别：</label>
                    <select class="form-control" v-model="edit_item.important">
                        <option v-for="p in importants" :value="p.id">{{p.name}}</option>
                    </select>
                </div>

                <div class="form-group ">
                    <label>更新内容描述：</label>
                    <textarea class="form-control" style="resize:vertical" v-model="edit_item.intro"></textarea>
                </div>

                <div class="form-group">
                    <a class="btn btn-primary" @click="save">保存</a>
                    <a class="btn btn-default" @click="show.edit = false">关闭</a>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->

<script>
    var Importants = [
        {id:1,name:'强制'},
        {id:2,name:'重要'},
        {id:3,name:'建议'},
    ];

    var app_version_app = new Vue({
        el: '#app-version-app',
        data: {
            list: [],
            edit_item: {},
            page: {
                last: 1,
                page: 1,
                rows: 10,
                platform: 0
            },
            show: {
                edit: false
            },
            importants: Importants
        },
        created: function () {
            this.getList();
        },
        methods: {
            getList: function () {
                ajax({
                    uri: '/app/version_list',
                    data: this.page,
                    success: function(resp){
                        resp.data.forEach(function (item, index) {
                            item.created = new Date(item.created).toLocaleString();
                        });
                        this.list = resp.data;
                        this.page.last = resp.last_page;
                    }.bind(this)
                });
            },
            add: function (platform) {
                this.edit_item = {important:1, platform: platform};
                this.show.edit = true;
            },
            edit: function (item) {
                this.edit_item = item;
                this.show.edit = true;
            },
            save: function () {
                var item = this.edit_item;
                if (!item.code || !item.version) {
                    alert('请完善资料！');
                    return;
                }

                ajax({
                    uri: '/app/version_save',
                    data: item,
                    success: function(resp){
                        console.log(resp);
                        this.show.edit = false;
                        this.getList();
                    }.bind(this)
                });
            },
            del: function (item) {
                ajax({
                    uri: '/app/version_del',
                    data: {id: item.id},
                    success: function (resp) {
                        this.getList();
                    }.bind(this)
                })
            },
            turnPage: function (offset) {
                this.page.offset = offset;
            }
        },
        filters: {
            Important: function (id) {
                for (var i=0; i<Importants.length; i++) {
                    var t = Importants[i];
                    if (t.id == id) {
                        return t.name;
                    }
                }
                return '';
            }
        },
        watch: {
            'page.offset': function (nv, ov) {
                this.getList();
            },
            'page.platform': function (nv, ov) {
                this.getList();
            }
        }
    });
</script>